/*! =======================================================
                      VERSION  1.1.0
 * ========================================================
 * webpack.config.default.js
 * ========================================================
 * 本文件为webpack基本配置文件
 * ======================================================== */
'use strict';
/** 执行合并打包任务 */
let path              = require('path'),
    webpack           = require('webpack'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    cssnano           = require('cssnano');

const ROOT_PATH = path.resolve(__dirname);
const APP_PATH = path.resolve(ROOT_PATH, 'src');
const BASE_CSS_LOADER = 'css?sourceMap&-minimize';

let excludePath = /(node_modules|dist)/;  // 排除扫描的文件夹
let is_build_local = process.argv.indexOf('--html')>-1;

let commonThunkarr=[
  'vue',
  'vue-router',
  'vue-resource',
  'vue-validator',
  'vuex',
  'lodash',
  'jquery',
  'IScroll',
  'Swiper'
];

module.exports = {
    // require('a.js')的时候从哪个路径查找
    context: joinPath('/src'),
    // 文件入口 entry
    entry: {
        // 通用模块配置
        common: commonThunkarr,
      	// app入口配置
      	app: []
    },
   	// 输出文件夹路径以及可被分块输出路径和名字
  	output: {
    	// 输出文件路径
      path: joinPath('dist'),
        // 公共路径输出配置
      publicPath: 'assets/',
        // 输出文件名字
    	filename: 'js/[name].bundle.js',
    	// 分块名称输出配置
    	chunkFilename: 'js/[id].[chunkhash].bundle.js'
    },
  	// 插件配置
    plugins: [
        new ExtractTextPlugin('css/[name].css', {
            allChunks : true
        }),
        // 允许错误不打断程序
        new webpack.NoErrorsPlugin(),
      	// 通用模块打包
        new webpack.optimize.CommonsChunkPlugin({
            names: ['common']
        }),
        // 比对id的使用频率和分布来得出最短的id分配给使用频率高的模块，使用OccurenceOrderPlugin減少文件大小
        new webpack.optimize.OccurenceOrderPlugin(),
        // 全局变量，无需每个模块引入
        new webpack.ProvidePlugin({
            $      : 'jquery',
            jQuery : 'jquery',
            _      : 'lodash'
        }),
        //设置环境变量
        /*new webpack.DefinePlugin({
            APP_EVN_TESTING:true,
            APP_EVN_STAGING:false,
            APP_EVN_PRODUCTION:false
        })*/
    ],
  	// module模块
  	module: {
      	// 模块loaders
      	loaders: [
            { test: /\.vue$/, loader: 'vue', exclude: excludePath },
            { test: /\.js$/,
               loader: 'babel',
              exclude: excludePath,
              include: [
                        // 只去解析运行目录下的 src 和 demo 文件夹
                        path.join(process.cwd(), './src'),
                        path.join(process.cwd(), './demo')
                      ]
            },
            { test: /\.css$/,
              loader:ExtractTextPlugin.extract('style','css?sourceMap&-minimize!postcss!sass?sourceMap',{publicPath:'../'}),
              // loaders: [
              // 'style',
              // BASE_CSS_LOADER,
              // 'postcss'
              // ],
              exclude: excludePath,
            },
            { test: /\.scss$/,
              loader:ExtractTextPlugin.extract('style','css?sourceMap&-minimize!postcss!sass?sourceMap',{publicPath:'../'}),
              // loaders: [
              //   'style',
              //   BASE_CSS_LOADER,
              //   'postcss',
              //   'sass?sourceMap'
              // ],
              exclude: excludePath,
              include: APP_PATH
            },
            { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192&name=images/[name].[ext]', exclude: excludePath },
            { test: /\.(woff|woff2|ttf|svg|eot|otf)$/, loader: 'file-loader?limit=8192&name=fonts/[name].[ext]', exclude: excludePath,include: APP_PATH}
        ]
    },
    postcss:[
      cssnano({
        autoprefixer: {
          add: true,
          remove: true,
          browsers: ['last 2 versions','Android >= 4.0']
        },
        discardComments: {
          removeAll: true
        },
        discardUnused: false,
        mergeIdents: false,
        reduceIdents: false,
        safe: true,
        sourcemap: true
      })
    ],
    // 其它解决方案配置
    resolve: {
        root: [joinPath('/src')],
        // 默认文件扩展名
        extensions: ['', '.js', '.json', '.scss'],
        // 定义别名
        // 依赖库别名配置
        alias:{
            'jquery' : joinPath('src/public/libs/jquery/jquery-1.10.1.min'),
            'load-image' : joinPath('src/public/plugins/jquery-file-upload/js/load-image.js'),
            'load-image-exif' : joinPath('src/public/plugins/jquery-file-upload/js/load-image-exif.js'),
            'load-image-meta' : joinPath('src/public/plugins/jquery-file-upload/js/load-image-meta.js'),
            'load-image-orientation' : joinPath('src/public/plugins/jquery-file-upload/js/load-image-orientation.js'),
            'canvas-to-blob' : joinPath('src/public/plugins/jquery-file-upload/js/canvas-to-blob.min.js'),
            'IScroll' : joinPath('src/public/libs/iscroll/iscroll-lite.js'),
            'Swiper'  : joinPath('src/public/plugins/swiper/js/swiper.jquery.umd.min.js')
        }
    },
  	babel: {
  		presets: ['es2015'],
  		plugins: ['transform-runtime']
  	},
    watch: false,  // 是否监听文件变化
};

/**
 * 合并路径
 * @function
 * @name  joinPath
 * @param {string} filePath 文件路径
 * @description 对当前模块目录路径与指定的文件路径进行合并
 */
function joinPath(filePath) {

    return path.join(__dirname, filePath);
}
